from selenium.webdriver.common.by import By
from pages.base_page import Page


class RealTimePage(Page):
    # Has feature
    ele_title = (By.ID, u'RealTime-title')

    ele_website_tab = (By.XPATH, u'//div/span[text() = "Website"]/..')
    ele_agent_tab = (By.XPATH, u'//div/span[text() = "Agent"]/..')
    ele_queue_tab = (By.XPATH, u'//div/span[text() = "Queue"]/..')

    ele_website_right_now_form = (By.XPATH, u'//div[text()="Right Now"]/..')
    ele_website_today_form = (By.XPATH, u'//div[text()="Today"]/..')

    ele_agent_site_button = (By.XPATH, u'//button[@value="site"]')
    ele_agent_department_button = (By.XPATH, u'//button[@value="department"]')

    ele_queue_table = (By.XPATH, u'//thead/tr')

    ele_error = (By.XPATH, '//div[@role="alert"]')

    # No feature
    ele_no_feature_title = (By.ID, u'realtime-title')
    ele_no_feature_image = (By.XPATH, u'//div/img[@alt="Realtime Introduction"]')
    ele_unlock_feature_button = (By.XPATH, u'//button/span[text()="Unlock This Feature"]/..')

    def __init__(self, driver, url, site_id):
        Page.__init__(self, driver, url)
        self.site_id = site_id
        self.url = url

    def check_page_has_error(self):
        try:
            flag = self.find_element(*self.ele_error).is_enabled()
            if flag:
                raise Exception("current page {} may has error".format(self.url))
        except Exception as e:
            return True

    def real_tine_page_eles_check_when_has_feature(self):
        self.find_element(*self.ele_title).is_enabled()
        self.find_element(*self.ele_website_tab).is_enabled()
        self.find_element(*self.ele_queue_tab).is_enabled()
        self.find_element(*self.ele_website_right_now_form).is_enabled()
        self.find_element(*self.ele_website_today_form).is_enabled()
        self.click(self.ele_agent_tab)
        self.find_element(*self.ele_agent_site_button).is_enabled()
        self.find_element(*self.ele_agent_department_button).is_enabled()
        self.click(self.ele_queue_tab)
        self.find_element(*self.ele_queue_table).is_enabled()

    def real_tine_page_eles_check_when_no_feature(self):
        self.find_element(*self.ele_no_feature_title).is_enabled()
        self.find_element(*self.ele_no_feature_image).is_enabled()
        self.find_element(*self.ele_unlock_feature_button).is_enabled()
